package com.shiyi.design.strategy;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author qiu
 * @Date 2021/1/11 3:39
 */
public class FireRouter {

    private FireRouter() {}

    private static final Map<String, FireStrategy> FIRE_STRATEGY_MAP = new HashMap<>();

    static {
        FireStrategyEnum[] fireStrategyEnums = FireStrategyEnum.values();
        if(fireStrategyEnums.length != 0) {
            for (FireStrategyEnum fireStrategyEnum : fireStrategyEnums) {
                String className = fireStrategyEnum.getClassName();
                FireStrategy fireStrategy = FIRE_STRATEGY_MAP.get(className);
                if(fireStrategy == null) {
                    try {
                        Class clazz = Class.forName(className);
                        FIRE_STRATEGY_MAP.put(className, (FireStrategy) clazz.newInstance());
                    } catch (Exception e) {
                        System.out.println(className + "类加载失败！e: " + e.getMessage());
                    }
                }
            }
        }
    }

    public static Map<String, FireStrategy> getMap() {
        return FIRE_STRATEGY_MAP;
    }
    public static FireStrategy route(String bizType) {
        FireStrategyEnum fireStrategyEnum = FireStrategyEnum.parse(bizType);
        if(fireStrategyEnum == null) {
            fireStrategyEnum = FireStrategyEnum.BULLET_FIRE_STRATEGY;
        }
        return FIRE_STRATEGY_MAP.get(fireStrategyEnum.getClassName());
    }
}
